package com.ti.demo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @description: com.ti.demo.controller
 * @Author Triagen
 * @Date 2025/7/18 10:53
 */
@RestController
public class ChatController {

    @Qualifier("shortMemoryChatClient")
    @Autowired
    private ChatClient shortMemoryChatClient;

    @Qualifier("longMemoryChatClient")
    @Autowired
    private ChatClient longMemoryChatClient;


    // Spring AI 的短期记忆最多 20 条
    @GetMapping("/shortMemory/chat")
    Flux<String> shortMemoryChat(String conversationId, String userInput) {
        return this.shortMemoryChatClient.prompt()
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId))
                .user(userInput)
                .stream()
                .content();
    }

    @GetMapping("/longMemory/chat")
    Flux<String> longMemoryChat(String conversationId, String userInput) {
        return this.longMemoryChatClient.prompt()
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId))
                .user(userInput)
                .stream()
                .content();
    }
}
